<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
import { postAgentConfig } from "./api/login";	
import Cookies from "js-cookie";
export default {
  name: 'App',
  data(){
  	return {
  		isSending:false,
  		token:'',
  		timer:'',
  	}
  },
  methods:{

// 	config(config) {
//		return new Promise((resolve, reject) => {
//			debugger
//			wx.config(config)
//			wx.ready(resolve)
//			wx.error(reject)
//		})
//	},
	fnGetAgentConfig(){
  		var self = this;
   		if(self.isSending == true){
   			console.log('请稍等');
   			return false;
   		}
   		self.isSending = true;
  	  postAgentConfig({url:location.href}).then((res) => {
  	  				
			      	self.isSending = false;
			        let { msg, code, data } = res.data;
			       	
								if(code == 200){
							
									wx.agentConfig({
										  debug: true,
									    corpid: data.corpid, // 必填，企业微信的corpid，必须与当前登录的企业一致
									    agentid: data.agentid, // 必填，企业微信的应用id （e.g. 1000247）
									    timestamp:data.timestamp, // 必填，生成签名的时间戳
									    nonceStr: data.nonceStr, // 必填，生成签名的随机串
									    signature: data.signature,// 必填，签名，见附录-JS-SDK使用权限签名算法
									    jsApiList: ["selectExternalContact"], //必填，传入需要使用的接口名称
											success: function(res) {
												console.log(res);
												WWOpenData.bind(self.$el)
										        // 回调
										  },
									    fail: function(res) {
													console.log(res);
									        if(res.errMsg.indexOf('function not exist') > -1){
									            alert('版本过低请升级')
									        }
									    }
										});
								}
								else{
									console.log(msg)
								}
			      },reject=>{
			   
			      	 self.isSending = false;
			      	 let { msg, code, data } = reject.data;
			      	 console.log(msg);
			      	 if(code == 401){
			      	 	Cookies.remove('token');
			      	 	self.$router.push('/login')
			      	 }
			      	 
			      })
	},
	agentConfig(config) {
			return new Promise((success, fail) => {
				wx.agentConfig({ ...config, success, fail })
			})
	
	}
  	
  },
  created(){


	
  },
  mounted(){
  	  var self = this;
  		self.token = Cookies.get('token');
	  	if(self.token){
				self.fnGetAgentConfig();
			}
	  	else{
	  		self.timer = setInterval(()=>{
	  			self.token = Cookies.get('token');
	  			if(self.token){
	  				clearInterval(self.timer);
	  				self.fnGetAgentConfig();
	  			}
	  			else{
	  				console.log('检测token 轮询')
	  			}
	  			
	  		},1000)
	  	}
	  }
}
</script>

<style>
	*{
		margin: 0;
		padding: 0;
	}
	html,body{
		width:100%;
	}
#app {
	width:100%;
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
	::-webkit-input-placeholder { 
	/* WebKit browsers，webkit内核浏览器 */
	color: #666 !important; 

	} 
	:-moz-placeholder { 
	/* Mozilla Firefox 4 to 18 */ 
	color: #666 !important; 

	} 
	::-moz-placeholder { 
	/* Mozilla Firefox 19+ */ 
	color: #666 !important; 

	} 
	:-ms-input-placeholder { 
	/* Internet Explorer 10+ */ 
	color: #666 !important; 

	}

	.no-touch {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
</style>
